package com.zh.note.huawei.twohundred.组合出合法最小数;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: zhanghuan
 * @date: 2023/8/11 09:14
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // String inputStr = sc.nextLine();
        // String[] strs = inputStr.split(" ");
        String inputStr = "20 1";
        String[] strs = inputStr.split(" ");
        Deque<String> path = new LinkedList<>();
        List<String> res = new ArrayList<>();
        boolean[] used = new boolean[strs.length];
        dfs(strs, 0, used, path, res);

        res.sort((o1, o2) -> o1.compareTo(o2));
        for (int i = 0; i < res.size(); i++) {
            if (!res.get(i).startsWith("0")) {
                System.out.println(res.get(i));
                return;
            }
        }
        System.out.println(res.get(0).substring(0, res.get(0).length()));


    }

    //求全排列
    private static void dfs(String[] strs, int depth, boolean[] userd, Deque<String> path, List<String> res) {
        if (depth == strs.length) {
            String collect = path.stream().collect(Collectors.joining(""));
            res.add(collect);
        }
        for (int i = 0; i < strs.length; i++) {
            if (userd[i]) {
                continue;
            }
            path.addLast(strs[i]);
            userd[i] = true;
            dfs(strs, depth + 1, userd, path, res);
            path.removeLast();
            userd[i] = false;
        }
    }
}
